package com.hnchances.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hnchances.entity.*;
import com.hnchances.mapper.*;
import com.hnchances.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnchances.vo.Result;
import com.hnchances.vo.SearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *

 */
@Service
public class GradesServiceImpl extends ServiceImpl<GradesMapper, Grades> implements GradesService {
    @Resource
    private StudentService studentService;
    @Resource
    private ExamService examService;
    @Resource
    private CourseService courseService;
    @Resource
    private ClassService classService;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ExamMapper examMapper;
    @Resource
    private ClassMapper classMapper;
    @Resource
    private CourseMapper courseMapper;

    @Override
    public Result add(Grades grades, User user) {
        //获取当前登录教师的班级
        Integer classid = user.getClassid();
        if (!grades.getClassid().equals(classid)){
            return Result.error().message("您无权操作其他班级的学生");
        }

        //判断这个学生是不是本班级的
        Student dbStudent = studentService.getById(grades.getSid());
        if (!user.getClassid().equals(dbStudent.getClassid())){
            return Result.error().message("您无权操作其他班级的学生");
        }
        Exam dbExam = examService.getById(grades.getExamid());
        if (dbExam==null){
            return Result.error().message("系统中不存在此考试");
        }
        Course dbCourse = courseService.getById(grades.getCourseid());
        if (dbCourse==null){
            return Result.error().message("系统中不存在此课程");
        }

        grades.setStatus(0);
        int i = baseMapper.insert(grades);
        if (i==1){
            return Result.ok().message("添加成绩成功");
        }
        return Result.error().message("添加失败，服务器存在异常");
    }

    @Override
    public Result update(Grades grades, User user) {
        int i = baseMapper.updateById(grades);
        if (i==1){
            return Result.ok().message("修改成绩成功");
        }
        return Result.error().message("修改失败，服务器存在异常");
    }

    @Override
    public Result delete(String id) {
        Grades grades = baseMapper.selectById(id);
        grades.setStatus(1);
        int i = baseMapper.updateById(grades);
        if (i==1){
            return Result.ok().message("删除成功");
        }
        return Result.error().message("删除失败，服务器存在异常");
    }

    @Override
    public Result search(SearchVo searchVo) {
        Page<Grades> pages = new Page<>(searchVo.getPageNumber(),searchVo.getPageCount());
        QueryWrapper<Grades> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getStuname())){
            Integer stuId = studentMapper.findByNameId(searchVo.getStuname());
            wrapper.eq("sid",stuId);
        }
        if (StringUtils.isNotEmpty(searchVo.getClassname())){
            Integer classId = classMapper.findByNameId(searchVo.getClassname());
            wrapper.eq("classID",classId);
        }
        if (StringUtils.isNotEmpty(searchVo.getCoursename())){
            Integer courseId = courseMapper.findByNameId(searchVo.getCoursename());
            wrapper.eq("courseID",courseId);
        }
        if (StringUtils.isNotEmpty(searchVo.getExamname())){
            Integer examId = examMapper.findExamId(searchVo.getExamname());
            wrapper.eq("examID",examId);
        }
        Page<Grades> gradesPage = baseMapper.selectPage(pages, wrapper);
        return Result.ok().data("data",gradesPage);
    }
}
